/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：FileInterfixContorller.java
 * History:
 *       May 13, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.contorller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.ProgressListener;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tangkf.utils.CommonUtils;
import com.tangkf.utils.bean.ReqHelper;
import com.tangkf.utils.file.FileUtils;
import com.xteam.ecsystem.utils.FileUploadServer;

/**
 * 文件处理枢纽站
 * 
 * @author liuzhi
 * 
 */
@Controller
public class FileInterfixContorller {

	String fileTypes = "xlsx|xls|bmp|png|jpg|gif|rar|zip|jar|cab|iso|tar|txt";
	long fileMaxSize = 256000000;

	public void setCharacterEncoding(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
	};
	/**
	 * 文件上传
	 * 
	 * @author ls
	 * @param request
	 * @param response
	 * @param modelMap
	 * @throws IOException
	 */
	@RequestMapping("/fileUploadContorller.do")
	public void fileUploadContorller(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setCharacterEncoding(request, response);

		String uploadType = ReqHelper.getRequestString(request, "uploadType");
		String uploadPath = ReqHelper.getRequestString(request, "uploadPath");

		FileUploadServer fus = new FileUploadServer(request, fileTypes,
				fileMaxSize);
		// 设置进度监听器
		fus.setProgressListerner(new ProgressListener() {
			public void update(long byteRead, long byteLength, int items) {
				System.err.println(byteRead + "-" + byteLength + "-" + items);
			}
		});

		List<FileItem> fis = fus.getFileItems();
		if (fis != null && fis.size() > 0) {
			for (FileItem fi : fis) {
				try {

					// 生成相对路径
					String pathYear = CommonUtils.getDateTime(new Date(),
							"yyyy");
					String pathMonth = CommonUtils
							.getDateTime(new Date(), "MM");
					String xdpath = "/" + pathYear + "/" + pathMonth;
					// 文件保存路径
					String filePath = uploadPath + xdpath;

					// 创建路径
					File dir = new File(filePath);
					FileUtils.makeDir(dir);
					String extName = fi.getName();
					extName = extName.substring(extName.lastIndexOf(".") + 1);

					// 生成文件名
					String fileName = java.util.UUID.randomUUID().toString()
							+ "." + extName;
					filePath = filePath + "/" + fileName;
					fus.saveFile(fi, filePath);
					System.out.println("{'imgPath':'" + xdpath + "/" + fileName
							+ "','imgName':'" + fi.getName() + "',"
							+ "'realPath':'" + uploadPath + "'}");

					if (uploadType != null && uploadType.equals("XX")) {
						response.getWriter().write("0");
					} else {
						 // 回写保存的相对路径
						 response.getWriter().write(
						 "{'imgPath':'" + xdpath + "/" + fileName + "',"
						 + "'imgName':'" + fi.getName() + "',"
						 + "'realPath':'" + uploadPath + "'}");
					}
					// break;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		// fus.clear();
	}

	/**
	 * 
	 * 文件下载
	 * 
	 * @author hasee
	 * @param request
	 * @param response
	 * @param modelMap
	 * @throws IOException
	 */
	@RequestMapping("/fileDownloadContorller.do")
	public void fileDownloadContorller(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setCharacterEncoding(request, response);
	}

	/**
	 * 删除文件
	 * 
	 * @author ls
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/fileDeleteContorller.do")
	public void fileDeleteContorller(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setCharacterEncoding(request, response);
		System.out.println("你已经成功删除了文件。");
	}

}
